import sys

from VideoWidget import *
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import *
from UI.openvideo import Ui_Form
import PyQt5.QtCore

class MainWindow(QWidget,Ui_Form):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)

        self.root_dir = r'./vido_source'

        self.FLAG_PLAY = False
        self.videoFullScreen = False  # 判断当前widget是否全屏

        self.widget = self.widget_v # 其实可以不要的，前期加上了，就不改了


        self.player = QMediaPlayer()
        self.videoSurface = VideoSurface()
        self.player.setVideoOutput(self.videoSurface)

        self.add_button()  # 添加画面旋转之类的

        # 设置进度条
        self.progressBar.setValue(0)
        self.play_time = self.player.duration()


        # 设置listview
        self.get_index()
        self.listView.setStyleSheet('''QListView::item
{
    padding-top:24px;
    padding-bottom:4px;
}''')

        # 信号
        self.pushButton.clicked.connect(self.play)
        self.pushButton_2.clicked.connect(self.stop)

        self.player.positionChanged.connect(self.changeSlide)
        self.widget_v.doubleClickedItem.connect(self.videoDoubleClicked)
        self.videoSurface.showImageSignal.connect(self.widget.showImageSlot)

    def add_button(self):
        button1 = QPushButton("向左旋转", self.widget)
        button1.setGeometry(0, 0, 100, 30)
        button1.clicked.connect(lambda: self.widget.setRotate(rotateToLeft))

        button2 = QPushButton("向右旋转", self.widget)
        button2.setGeometry(0, 150, 100, 30)
        button2.clicked.connect(lambda: self.widget.setRotate(rotateToRight))

        button3 = QPushButton("水平翻转", self.widget)
        button3.setGeometry(150, 0, 100, 30)
        button3.clicked.connect(lambda: self.widget.setFlip(horizontalFlip))

        button4 = QPushButton("垂直翻转", self.widget)
        button4.setGeometry(150, 150, 100, 30)
        button4.clicked.connect(lambda: self.widget.setFlip(verticalFlip))

    def get_index(self):
        path = self.root_dir
        self.fileModel = QFileSystemModel(self)
        self.fileModel.setRootPath(path)
        self.fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files)
        self.listView.setModel(self.fileModel)
        self.listView.setRootIndex(self.fileModel.index(path))
        self.listView.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.listView.doubleClicked.connect(self.choose_file)

    def choose_file(self,info):
        self.player.setMedia(QMediaContent(QUrl.fromLocalFile(self.root_dir+'//'+info.data())))
        self.play()


    def play(self):
        if not self.FLAG_PLAY:
            self.player.play()
            self.FLAG_PLAY = True
            print('播放')
        else:
            self.player.pause()
            self.FLAG_PLAY = False

    def stop(self):
        self.player.pause()

    def changeSlide(self,position):
        self.vidoeLength = self.player.duration() + 0.1
        print((position / self.vidoeLength) * 1000)
        self.progressBar.setValue(round((position / self.vidoeLength) * 100))

    def videoDoubleClicked(self):
        if self.player.duration() > 0:  # 开始播放后才允许进行全屏操作
            if self.videoFullScreen:
                # self.player.pause()
                # self.videoFullScreenWidget.hide()
                # self.player.setVideoOutput(self.wgt_video)
                # self.player.play()
                self.videoFullScreen = False
                self.widget_v.setFullScreen(0)
                self.widget_v.setMaximumSize(self.wsize)
            else:
                self.wsize = self.widget_v.size()
                self.widget_v.setFullScreen(1)
                # self.player.pause()
                # self.videoFullScreenWidget.show()
                # self.player.setVideoOutput(self.videoFullScreenWidget)
                # self.player.play()
                self.videoFullScreen = True




# 运行前先运行 'Receiver.py'
if __name__ == "__main__":
    import sys
    from PyQt5.QtCore import Qt

    # 高分辨率屏幕适应
    if hasattr(Qt, 'AA_EnableHighDpiScaling'):
        QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
    if hasattr(Qt, 'AA_UseHighDpiPixmaps'):
        QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)

    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())
